package com.lims.landInfo.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javacommon.base.*;
import javacommon.util.TextTool;

import cn.org.rapid_framework.page.*;

import com.lims.landInfo.model.*;
import com.lims.landInfo.dao.*;
import com.lims.landInfo.vo.query.*;

/**
 * @author zzh
 * @version 1.0
 * @since 1.0
 */@Service
@Transactional @SuppressWarnings("unchecked")
public class DisposalManager extends BaseManager<Disposal,java.lang.String>{

	private DisposalDao disposalDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setDisposalDao(DisposalDao dao) {
		this.disposalDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.disposalDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(DisposalQuery query) {
		return disposalDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public ListRange findRange(DisposalQuery query) {
		Page page = this.disposalDao.findPage(query);
		ListRange listRange = new ListRange();
		listRange.setRows(page.getResult().toArray());
		listRange.setTotal(page.getTotalCount());
		return listRange;
	}
	
	/**
	 * 验证并保存数据
	 * @return
	 */
	public List saveAndCheckImpData(Collection entities,Date landDate) {
		List errList = new ArrayList(0);
		Date curDate = new Date(System.currentTimeMillis());
		for (Iterator iterator = entities.iterator(); iterator.hasNext();) {
			Disposal disposal = (Disposal) iterator.next();
			disposal.setCreateDate(curDate);
			disposal.setLandDate(landDate);
			String errMsg = checkLand(disposal);
			if(TextTool.isNotNull(errMsg)) {
				disposal.setDescribe(errMsg);
				errList.add(disposal);
			} else {
				this.disposalDao.save(disposal);
			}
		}
		return errList; 
	}
	
	/**
	 * 验证
	 * 返回若为空，验证通过，若不为空，返回验证失败原因
	 * @return
	 */
	private String checkLand(Disposal disposal) {
		String msg = "";
//		if(TextTool.isNull(disposal.getLandno())) {
//			msg += "宗地编号为空。";
//		} else{
//			String hql = "from Disposal l where l.landno=?";
//			List landList = this.disposalDao.find(hql, disposal.getLandno());
//			if(TextTool.isNotNull(landList)) {
//				String oldYear = disposal.getLandDateString().substring(0, 4);
//				String newYear = ((Disposal)landList.get(0)).getLandDateString().substring(0,4);
//				if(oldYear.equals(newYear)) {
//					msg += "宗地编号重复。";
//				}
//			}
//		}
		return msg;
	}
	
}
